home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / jpeg / jrdsgi.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  152 lines

  1. /*
  2.  * jrdsgi.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in SGI format.
  9.  *
  10.  * These routines use the SGI image library functions found in -limage
  11.  *
  12.  *                Paul Haeberli - 1993
  13.  */
  14.  
  15. #include "jinclude.h"
  16.  
  17. #ifdef SGI_SUPPORTED
  18. #include "gl/image.h"
  19.  
  20. #define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  21.  
  22. LOCAL short *rbuf;
  23. LOCAL short *gbuf;
  24. LOCAL short *bbuf;
  25. LOCAL int cury;
  26. LOCAL IMAGE *image;
  27.  
  28. /*
  29.  * Read the file header; return image size and component count.
  30.  */
  31. LOCAL int isimagemagic(magic)
  32. unsigned short magic;
  33. {
  34.     if(magic == IMAGIC)
  35.         return 1;
  36.     magic = (magic>>8)&0xff + (magic<<8)&0xff00;
  37.     if(magic == IMAGIC)
  38.         return 1;
  39.     else
  40.         return 0;
  41. }
  42.  
  43. /*
  44.  * Read one row of pixels.
  45.  * This version is used for B/W SGI images:
  46.  */
  47. METHODDEF void
  48. get_BW_pixel_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  49. /* This version is for reading 8-bit grayscale pixels */
  50. {
  51.   register JSAMPROW ptr0;
  52.   register long col, width;
  53.  
  54.   getrow(image,rbuf,image->ysize-1-cury,0);
  55.   ptr0 = pixel_row[0];
  56.   width = cinfo->image_width;
  57.   for (col = 0; col<width; col++) 
  58.     *ptr0++ = (JSAMPLE)rbuf[col];
  59.   cury++;
  60. }
  61.  
  62. /*
  63.  * Read one row of pixels.
  64.  * This version is used for B/W SGI images:
  65.  */
  66. METHODDEF void
  67. get_RGB_pixel_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  68. /* This version is for reading 8-bit grayscale pixels */
  69. {
  70.   register JSAMPROW ptr0, ptr1, ptr2;
  71.   register long col, width;
  72.  
  73.   getrow(image,rbuf,image->ysize-1-cury,0);
  74.   getrow(image,gbuf,image->ysize-1-cury,1);
  75.   getrow(image,bbuf,image->ysize-1-cury,2);
  76.   ptr0 = pixel_row[0];
  77.   ptr1 = pixel_row[1];
  78.   ptr2 = pixel_row[2];
  79.   width = cinfo->image_width;
  80.   for (col = 0; col<width; col++) {
  81.     *ptr0++ = (JSAMPLE)rbuf[col];
  82.     *ptr1++ = (JSAMPLE)gbuf[col];
  83.     *ptr2++ = (JSAMPLE)bbuf[col];
  84.   }
  85.   cury++;
  86. }
  87.  
  88. METHODDEF void
  89. input_init (compress_info_ptr cinfo)
  90. {
  91.   unsigned char hdrbuf[2];    /* workspace for reading control blocks */
  92.  
  93.   /* Read and verify SGI Header */
  94.   if (! ReadOK(cinfo->input_file, hdrbuf, 2))
  95.     ERREXIT(cinfo->emethods, "Can't read file");
  96.   if (! isimagemagic((hdrbuf[0]<<8)+hdrbuf[1]) )
  97.     ERREXIT(cinfo->emethods, "Not an SGI file");
  98.   lseek(fileno(cinfo->input_file),0L,0);
  99.   image = (IMAGE *)fiopen(fileno(cinfo->input_file),"r");
  100.   if (! image )
  101.     ERREXIT(cinfo->emethods, "Error on SGI image open");
  102.  
  103.   /* Return info about the image. */
  104.   if(image->zsize<3) {
  105.      cinfo->input_components = 1;
  106.      cinfo->in_color_space = CS_GRAYSCALE;
  107.      cinfo->methods->get_input_row = get_BW_pixel_row;
  108.   } else {
  109.      cinfo->input_components = 3;
  110.      cinfo->in_color_space = CS_RGB;
  111.      cinfo->methods->get_input_row = get_RGB_pixel_row;
  112.   }
  113.   cinfo->image_width = image->xsize;
  114.   cinfo->image_height = image->ysize;
  115.   cinfo->data_precision = 8;
  116.  
  117.   rbuf = (short *)malloc(image->xsize*sizeof(short));
  118.   gbuf = (short *)malloc(image->xsize*sizeof(short));
  119.   bbuf = (short *)malloc(image->xsize*sizeof(short));
  120.   cury = 0;
  121.   TRACEMS2(cinfo->emethods, 1, "%ux%ux SGI image",
  122.        (unsigned int) image->xsize, (unsigned int) image->ysize);
  123. }
  124.  
  125. /*
  126.  * Finish up at the end of the file.
  127.  */
  128. METHODDEF void
  129. input_term (compress_info_ptr cinfo)
  130. {
  131.    free(image);
  132.    free(rbuf);
  133.    free(gbuf);
  134.    free(bbuf);
  135. }
  136.  
  137. /*
  138.  * The method selection routine for SGI format input.
  139.  * Note that this must be called by the user interface before calling
  140.  * jpeg_compress.  If multiple input formats are supported, the
  141.  * user interface is responsible for discovering the file format and
  142.  * calling the appropriate method selection routine.
  143.  */
  144. GLOBAL void
  145. jselrsgi (compress_info_ptr cinfo)
  146. {
  147.   cinfo->methods->input_init = input_init;
  148.   cinfo->methods->input_term = input_term;
  149. }
  150.  
  151. #endif /* SGI_SUPPORTED */
  152.